﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBHelper;
using System.Data.SqlClient;
using System.Data;
using BusinessLayer.FindArtInfo.Entities;
using BusinessLayer.FindArtInfo.Models;

namespace BusinessLayer.FindArtInfo
{
    public class PriceBL
    {        
        private readonly DbHelper _db;

        public PriceBL()
        {
            _db = new DbHelper(new DbConfig().DbFindArtInfoConnectionString);         
        }

        public void GetCountByKeyWord(string keyWord, out int totalRecord)
        {
            var oCommand = new SqlCommand("sp_Price_Select_Count_By_KeyWord") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@KeyWord", keyWord));            
            var output = new SqlParameter("@TotalRecord", SqlDbType.Int) { Direction = ParameterDirection.Output };
            oCommand.Parameters.Add(output);                                              
            _db.ExecuteNonQuery(oCommand);
            totalRecord = (int)output.Value;            
        }        

        public List<PriceModel> Search(string keyWord, int pageIndex, int pageSize)
        {
            var oCommand = new SqlCommand("sp_Price_Search") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@KeyWord", keyWord));            
            oCommand.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            oCommand.Parameters.Add(new SqlParameter("@PageSize", pageSize));        
            var list = _db.GetList<PriceModel>(oCommand);            
            return list;
        }

        public void GetCountByArtistsId(int artistsId, out int totalRecord)
        {
            var oCommand = new SqlCommand("sp_Price_Select_Count_By_ArtistsId") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@ArtistsId", artistsId));            
            var output = new SqlParameter("@TotalRecord", SqlDbType.Int) { Direction = ParameterDirection.Output };
            oCommand.Parameters.Add(output);
            _db.ExecuteNonQuery(oCommand);
            totalRecord = (int)output.Value;
        }        

        public List<PriceModel> GetByArtistsId(int artistsId, int pageIndex, int pageSize)
        {
            var oCommand = new SqlCommand("sp_Price_Select_By_ArtistsId") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@ArtistsId", artistsId));            
            oCommand.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            oCommand.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            var list = _db.GetList<PriceModel>(oCommand);
            return list;
        }

        public PriceDetailModel GetById(int id)
        {
            var oCommand = new SqlCommand("sp_Price_Select_By_Id") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Id", id));
            var list = _db.GetList<PriceDetailModel>(oCommand);
            return list.FirstOrDefault();
        }
    }
}
